<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class NewsController extends CommonController {

	protected $statusArr=array("下架","上架");
	protected $isHome=array("未推荐","已推荐");
	protected $typeArr = array(0=>'公告',1=> '消息');
	
    public function index(){
		header('Content-Type:text/html;charset=utf-8');
		
		//-----------------搜索-------------------
	
		$sh_q = I('sh_q','');//关键字
		if(!empty($sh_q)){
			$shArray['sh_q'] = $sh_q;
			$map['id'] = $sh_q;
			$map['title'] = array('LIKE','%'.$sh_q.'%');
			$map['content'] = array('LIKE','%'.$sh_q.'%');
			$map['_logic'] = 'OR';
			$where['_complex'] = $map;
		}


		$from_time = I('from_time','');
		$to_time = I('to_time','');
		  
		if($from_time){
			$from_time_s = strtotime($from_time);
			$where['addtime']= array('EGT',$from_time_s);
			$shArray['from_time'] = $from_time;
		}
		
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['addtime']= array('ELT',$to_time_s);
			$shArray['to_time'] = $to_time;	
		}		
		
		if($from_time && $to_time){
			$where['add_time']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}	
			  
		if(!empty($shArray)){$this->assign('shArray',$shArray);}// 搜索参数输出
		//-------------------------搜索end-----------------

		$pageno = I('p',1);
                $pageCount = 10;
		$list = M('shop_news')->where($where)->order('id desc')->page($pageno.','.$pageCount)->select();
		foreach( $list as $key => $item){
		
		$list[$key]['author_name'] = M('administrator')->where('id="'.$item['author'].'"')->getField('username');
		
		}
		$this->assign('list',$list);
		$count = M('shop_news')->where($where)->count();
		$this->assign('count',$count);
		$Page = new \Think\Page($count,$pageCount);
		$show = $Page->showAdmin();// 分页显示输出
		$this->assign('page',$show);// 赋值分页输出	
                $this->assign('statusArr',$this->statusArr);// 状态
		$this->display('News/index');
    }
	
       public function editStatus(){
	         $cid = I('cid');
		 $status = I('state');
	         $dataInfo = M('shop_news')->find($cid);
		 
                if( $dataInfo ){
                   if( $status == 1){
                          $data['status'] = 0;
                        }else{
                           $data['status'] = 1;
                        }
                        $result = M('shop_news')->where('id="'.$dataInfo['id'].'"')->save($data);
                }

                if( $result ){
                    $info = 100;
                    $this->ajaxReturn($info);
                 }
	 
	 }
	 
  public  function edit(){
   header('Content-Type:text/html;charset=utf-8');
	  $id = I('id');
	  $dataInfo = M('shop_news')->where('id="'.$id.'"')->find($id);
	  $typeArr  = array(0=>'公告',1=>'消息');
	  if( $dataInfo ){
		 $this->assign('dataInfo', $dataInfo);
		 $this->assign('typeArr', $typeArr);
		 $this->display();
	  }

	}
	
    public  function  saveEdit(){
      	 $data['title']= I('title');
         $data['type_id'] = I('colid');
         $data['content'] = I('content');
         $id = I('updateid');
         if($data){
	     $result = M('shop_news')->where('id="'.$id.'"')->save($data);
	  }
		 
         if($result){
                $info['status'] =100;
                $info['info'] = '修改成功';
                $this->ajaxReturn($info);
	   }else{
                $info['status'] =101;
                $info['info'] = '修改失败';
                $this->ajaxReturn($info);
	   }
       
	    

  }	
  
  
    public function add(){
	header('Content-Type:text/html;charset=utf-8');
        $typeArr  = array(0=>'公告',1=>'消息');
	$this->assign('typeArr', $typeArr);
        $this->display();
      }
	
	
  public  function save(){
       $data['title']= I('title');
       $data['type_id'] = I('colid');
       $data['content'] = I('content');
       $data['author'] = $_SESSION['admin_user_id'];
       $data['add_time'] = time();
       if( $data ){
       $result = M('shop_news')->add($data);		 
       }
       if( $result ){
       $info['status'] = 100;
       $info['info'] = '添加成功';
       $this->ajaxReturn($info);
        }
   }
   
     public function del(){

            $id = $_REQUEST['id'];
			
            if (is_array($id)){
               $where = 'id in('.implode(',', $id).')';
            } else {
               $where = 'id='.$id;
            }
	    $list = M('shop_news')->where($where)->delete();
	    $this->redirect('News/index');
            
    }
	
	
	 
}